var https = require('https');
var http = require('http');
var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');

//初始url
var url = "http://www.huya.com/longdd";
var url1 = "http://www.huya.com/521109";
var url2 = "https://www.douyu.com/9999";
var url3 = "http://www.huomao.com/517808";
var url4 = "https://live.bilibili.com/888";

var zhiboCheck = {

    /**
     * 调用统一方法
     * @param url
     * @returns {*|boolean|*}
     */
    getStatuc : function(url){
        // 检测是否需要修改 url
        // 获取请求数据

        var status = zhiboCheck.getRequst(url);

        if (typeof status == "object"){
            return ajaxRes(-1,'获取直播状态异常!');
        }

        console.log('aaa');
        return false;

        if (status){
            console.log('正在直播');
        }else{
            console.log('未在直播');
        }

        return status;
    },
    getRequst : function(x,isDouyu) {

        // 判断是否为https
        var sendStpe = http;
        isDouyu =  typeof isDouyu == "undefined" ? false : true;

        if (x.indexOf("https://") > -1) {
            sendStpe = https;
        }

        //采用http模块向服务器发起一次get请求
        sendStpe.get(x, function (req, res) {
            var html = '';        //用来存储请求网页的整个html内容

            req.setEncoding('utf-8'); //防止中文乱码
            //监听data事件，每次取一块数据
            req.on('data', function (chunk) {
                html += chunk;
            });
            //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
            req.on('end', function () {

                var webSite = '';

                if (x.indexOf("www.huya.com") > 0){
                    webSite = 'huya';

                }
                else if (x.indexOf("www.douyu.com") > 0){

                    webSite = 'douyu';

                    /**
                     * 判断是否为二次进入
                     */
                    if (!isDouyu){

                        var $ = cheerio.load(html); //采用cheerio模块解析html
                        var roomObj = $('.Title-anchorName').attr('href');

                        if (!roomObj){
                            return ajaxRes(-1,'获取状态失败!');
                        }

                        var room_id = roomObj.split('=')[1];

                        isDouyu = true;
                        var url = 'https://www.douyu.com/betard/'+room_id;
                        zhiboCheck.getRequst(url,isDouyu);
                        return false;
                    }

                }

                else if (x.indexOf("www.huomao.com") > 0){
                    webSite = 'huomao';

                }
                else if (x.indexOf("live.bilibili.com") > 0){
                    webSite = 'bilibili';

                }else{
                    return ajaxRes(-1,'判断直播平台有误!');
                }

                return zhiboCheck[webSite](html);

            });
        }).on('error', function (err) {
            console.log(err);
        });

    },

    /**
     * 分析直播状态
     * @param dom
     * @returns {boolean}
     */
    huya : function (dom) {

        var $ = cheerio.load(dom); //采用cheerio模块解析html

        if ($('.host-prevStartTime >span').length == 0){
            return true;
        }
        return false;
    },

    /**
     * 分析直播状态
     * @param dom
     * @returns {boolean}
     */
    douyu :function (dom) {

        if (typeof dom != "string"){
            return ajaxRes(-1,'解析数据有误');
        }


        var obj = JSON.parse(dom);
        if (obj.room.show_status == 1){
            ajaxRes(0,'正在直播');
        }
        ajaxRes(-1,'未在直播');
    }

};

/**
 * 数据返回函数
 * @param code
 * @param message
 * @param data
 * @returns {{code: *, message: *, data: *}}
 */
function ajaxRes(code,message,data){

    data = typeof data == "undefined" ? '' : data;

    return {
      'code':code,
      'message':message,
      'data':data,
    };
}

zhiboCheck.getRequst('https://www.douyu.com/74960');  //主程序开始运行
